auto();
//console.show();控制台
var dW=device.width;
var dH=device.height;
var textId="b_l";  //朋友圈文字id
var musicId="dm3"; //朋友圈音乐id
var tryCount=1;
setScreenMetrics(dW,dH);
launch("com.tencent.mm");
sleep(1000);
log("屏幕宽:"+dW+"高:"+dH);
events.observeKey();
toast("脚本开始运行，按音量上键强制停止脚本");
sleep(1000);
var thread = threads.start(function(){
	while(desc("查看消息").findOne()==null||text("今天").findOne()==null){
	   toast("请进入朋友圈界面");
	   sleep(500);
    }
    toast("已检测到进入朋友圈界面，开始删除");
    while(true){
        if(desc("查看消息").findOne(500)==null){
            toast("程序已结束运行");
		    exit();
		    break;
	    }
	    var textMsg=id(textId).findOne(500);
        if(textMsg!=null){
		    log("文字");
	        click(textMsg.bounds().centerX(), textMsg.bounds().centerY());
	        doDelete();
            continue;
	    }
	    log("找不到文字");
	    var photo=desc("图片").findOne(500);
	    if(photo!=null){
	        click(photo.bounds().centerX(), photo.bounds().centerY());
	        sleep(500);
            click(dW-150, dH-50);
		    sleep(250);
            doDelete()
            if(desc("查看消息").findOne()==null){
		     back();
	        }
		    continue;
	    }
	    log("找不到图片");
        var music=id(musicId).findOne(500);
        if(music!=null){
            click(music.bounds().centerX(), music.bounds().centerY());
            doDelete();
            continue;
	    }
	    log("找不到音乐");
	    if(photo==null&&textMsg==null&&music==null){
			toast("找不到可删除项目 尝试下滑第"+tryCount+"次");
		    if(tryCount>3){
			    toast("程序已结束运行");
		        exit();
		        break;
		    }
		    swipe(dW/2,dH/2,dW/2,dH/2-100,1000);
		    tryCount++;
		    log("下滑");
		}
    }
});

events.onKeyDown("volume_up", function(event){
	 toast("程序已结束运行");
	 thread.interrupt();
     exit();
});

function doDelete(){
    var del=text("删除").findOne(500);
    if(del!=null){
        click(del.bounds().centerX(), del.bounds().centerY());
    }
    var cancel= text("取消").findOne(250);
    var submit = text("确定").findOne(250);
    var tips= text("提示").findOne(250);
    if(cancel!=null&&submit!=null&&tips!=null){
        click(submit.bounds().centerX(), submit.bounds().centerY());
        sleep(1000);
	}
}